using System.Threading;
using System.Threading.Tasks;
using UnityEngine;

namespace EntityKit.Runtime
{
    public static partial class AudioKit
    {
        /// <summary>
        /// 背景音乐音量
        /// </summary>
        public static float BackgroundVolume
        {
            get => backgroundVolume;
            set
            {
                if (backgroundSource != null) backgroundSource.volume = backgroundVolume = Mathf.Clamp01(value);
            }
        }

        public static bool IsBackgroundPlaying => backgroundSource != null && backgroundSource.isPlaying;

        /// <summary>
        /// 静音控制
        /// </summary>
        public static bool BackgroundMute
        {
            get => isBackgroundMute;
            set
            {
                if (isBackgroundMute == value) return;
                isBackgroundMute = value;
                if (backgroundSource != null) backgroundSource.mute = value;
            }
        }

        /// <summary>
        /// 播放背景音乐
        /// </summary>
        /// <param name="clip">音频剪辑</param>
        /// <param name="isLoop">是否循环</param>
        /// <param name="speed">播放速度</param>
        public static void PlayBackgroundMusic(AudioClip clip, bool isLoop = true, float speed = 1)
        {
            if (!isInitialized || !ValidateClip(clip)) return;

            ConfigureAudioSource(backgroundSource, clip, isLoop, speed);
            if (backgroundSource != null)
            {
                backgroundSource.Play();
            }
        }

        /// <summary>
        /// 停止播放背景音乐
        /// </summary>
        public static void StopBackgroundMusic()
        {
            if (backgroundSource != null)
            {
                backgroundSource.Stop();
            }
        }

        /// <summary>
        /// 暂停播放背景音乐
        /// </summary>
        public static void PauseBackgroundMusic()
        {
            if (backgroundSource != null)
            {
                backgroundSource.Pause();
            }
        }

        /// <summary>
        /// 恢复播放背景音乐
        /// </summary>
        public static void ResumeBackgroundMusic()
        {
            if (backgroundSource != null)
            {
                backgroundSource.UnPause();
            }
        }


        private static void BackgroundMusicInitialize()
        {
            backgroundSource = CreateAudioSource("BackgroundSource", backgroundVolume, 1, 0, root, isBackgroundMute);
            if (backgroundSource != null)
            {
                backgroundSource.loop = true;
                BackgroundVolume = backgroundVolumeDefault;
            }
        }

        private static void BackgroundMusicUpdate()
        {
        }

        private static void BackgroundMusicRelease()
        {
            if (backgroundSource != null)
            {
                Object.Destroy(backgroundSource.gameObject);
                backgroundSource = null;
            }
        }

        private static AudioSource backgroundSource;
        private static float backgroundVolumeDefault = 0.6f;
        private static float backgroundVolume;
        private static bool isBackgroundMute;
    }
}